package other.edan.pass;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.ScriptTrigger;
import com.rkhd.platform.sdk.data.model.Account;
import com.rkhd.platform.sdk.data.model.Department;
import com.rkhd.platform.sdk.data.model.Opportunity;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.DataModel;
import com.rkhd.platform.sdk.model.OperateResult;
import com.rkhd.platform.sdk.param.ScriptTriggerParam;
import com.rkhd.platform.sdk.param.ScriptTriggerResult;
import other.edan.pass.utils.ComUtils;
import other.edan.pass.utils.DateTools;
import other.edan.pass.utils.HCXObjectService;

/**功能描述：添加商机后，更新商机名称
 * @ClassName: OpportunityAfterAddOrUpdateTri
 * @Description:终端客户+的+适配产品方案（单品：产品名称；多品：一批）+医疗设备采购+日期
 * @author 范伟绵
 * @date 2024年06月05日
 */

public class OpportunityAfterAddOrUpdateTri implements ScriptTrigger {
	private static final Logger LOGGER = LoggerFactory.getLogger();
	private static final String LOG_MSG_EX="新增/修改商机后：";
	
	@Override
	public ScriptTriggerResult execute(ScriptTriggerParam arg0) throws ScriptBusinessException {
		LOGGER.debug(LOG_MSG_EX+"开始");
		JSONObject opporJson = null;
		try {
			List<DataModel> dmList = arg0.getDataModelList();
			for (DataModel dm: dmList) {
				opporJson = JSONObject.parseObject(dm.toString());
				LOGGER.info("商机信息:" + opporJson.toJSONString());
			}
			rewriteName(opporJson);
			return new ScriptTriggerResult(dmList);
		} catch (Exception e) {
			LOGGER.info(e.getMessage());
			throw new ScriptBusinessException("添加/修改【商机】保存时发生异常：" + e.getMessage());
		}
	}

	private  void  rewriteName(JSONObject opporJson) throws IOException {
		String opporName="";
		Opportunity updateOppor=new Opportunity();
		updateOppor.setId(opporJson.getLong("id"));

		Account account=new Account();
		account.setId(opporJson.getLong("accountId"));
		account=HCXObjectService.get(account);
		if(null==account){
			LOGGER.info("获取终端客户信息失败");
			return;
		}
		LOGGER.info("终端客户信息："+account.toString());

		Long createdAt=opporJson.getLong("createdAt");
		String date=DateTools.longToString(createdAt,"yyyy-MM-dd");

		JSONArray productLineGlobalPick = ComUtils.getGlobalPicks("globalPick19__c");       	//产品线通用选项集
		JSONArray productLineArray= opporJson.getJSONArray("adaptiveProductSolutions__c");
		if(null!=productLineArray) {
			LOGGER.info("适配产品方案：" + productLineArray.toJSONString());
		}
		String productName="";
		int size=(null==productLineArray)?0:productLineArray.size();
		LOGGER.info("size="+size);
		switch (size){
			case 0:{
				opporName=account.getAccountName()+"的某某医疗设备采购"+date;
				break;
			}
			case 1:{
				Integer line=productLineArray.getInteger(0);
				productName=ComUtils.getOptionOptionLabel(productLineGlobalPick,line);
				opporName=account.getAccountName()+"的"+productName+"医疗设备采购"+date;
				break;
			}
			default:{
				productName="一批";
				opporName=account.getAccountName()+"的"+productName+"医疗设备采购"+date;
				break;
			}
		}
		updateOppor.setOpportunityName(opporName);

		LOGGER.info("更新的商机信息:"+updateOppor);
		OperateResult result=HCXObjectService.update(updateOppor,true);
		LOGGER.info("更新结果:"+result.getSuccess()+",信息:"+result.getErrorMessage());
	}

	public static void main(String[] args) throws IOException {
		/*Opportunity opportunity=HCXObjectService.getById("opportunity",3314457468820120L);
		JSONObject jsonObject=JSONObject.parseObject(opportunity.toString());
		System.err.println(jsonObject);

		OpportunityAfterAddOrUpdateTri api=new OpportunityAfterAddOrUpdateTri();
		api.rewriteName(jsonObject);*/

		/*{
			"pickOptionData": [
			{
				"optionLabel": "最终测试"
			},
			{
				"optionLabel": "最终测试2"
			},
			{
				"optionLabel": "最终测试3"
			}
  ]
		}*/

		List<String> county=new ArrayList<>();
		county.add("福清市");
		county.add("鼓楼区");
		county.add("台江区");
		county.add("仓山区");
		/*county.add("马尾区");
		county.add("晋安区");
		county.add("长乐区");*/

		JSONObject dataJson=new JSONObject();
		JSONArray array=new JSONArray();
		for (String name:county){
			JSONObject tmp=new JSONObject();
			tmp.put("optionLabel",name);
			array.add(tmp);
		}
		dataJson.put("pickOptionData",array);

		ComUtils.addGlobalPicks("globalPick40__c",dataJson);
	}
}
